<template>
  <!-- 添加或修改电子签章对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="80vw">
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <form-block-title title="签章信息"/>
      <el-row>
        <el-col :span="10">
          <el-form-item label="用户姓名" prop="userName">
            <div class="form-control-readonly">{{ form.userName }}</div>
          </el-form-item>
        </el-col>
      </el-row>

      <form-block-title title="电子签章"/>
      <el-row>
        <el-col :span="20">
          <el-form-item label="选择图片" prop="upload">
            <attachment ref="attachment"
                        :business-id="form.id"
                        module-name="电子签章"
                        uploader-id="electronicSignature"
                        @onFileSuccess="(uploadAttachmentIds)=>this.form.searchValue=uploadAttachmentIds">
            </attachment>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  addElectronicSignature,
  getElectronicSignature,
  updateElectronicSignature
} from '@/api/system/electronicSignature'
import FormBlockTitle from '@/components/ScFormBlockTitle/index'
import {getUserProfile} from '@/api/system/user'
import Attachment from '../../../components/ScAttachment/index'

export default {
  name: 'Edit',
  components: {Attachment, FormBlockTitle},
  data() {
    return {
      id: -1,
      // 弹出层标题
      title: '录入信息',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
  },
  methods: {
    /**
     * 获取当前登录用户
     */
    getUserProfile() {
      getUserProfile().then(response => {
        if (response.data) {
          this.form.userId = response.data.userId
          this.form.userName = response.data.userName
        }
      })
    },
    init(id) {
      //编辑页面加载数据
      if (id && id !== -1) {
        this.id = id
        this.form.id = id

        getElectronicSignature(id).then(response => {
          this.form = response.data

          if (this.$refs['attachment']) {
            this.$refs['attachment'].setBusinessId(id)
            this.$refs['attachment'].initFileList()
          }
        })
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createById: null,
        deptId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userId: null,
        userName: null
      }

      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateElectronicSignature(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            addElectronicSignature(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.$emit('refreshDataList')
              }
            })
          }
        }
      })
    }
  }
}
</script>
